/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rie06.rmi;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;

/**
 * REST Web Service
 *
 * @author clement
 */
public class WS_Live {

    @Context
    private UriInfo context;
    /**
     * Creates a new instance of WS_Live
     */
    private static String xmlhead = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?><monitoring>";
    private static String xmlfoot = "</monitoring>";
    private String serverIP;

    public WS_Live(String server_ip) {
        this.serverIP = server_ip;
        System.out.println("Server IP: " + serverIP);
    }

    /**
     * Retrieves representation of an instance of com.rie06.jaxrsws.WS_Live
     *
     * @return an instance of java.lang.String
     */
    @GET
    @Produces("application/xml")
    public String getXml() {
        return result(serverIP, false, false, false);
    }

    @Path("cpu")
    @GET
    @Produces("application/xml")
    public String getCPU() {
        return result(serverIP, true, false, false);
    }

    @Path("memory")
    @GET
    @Produces("application/xml")
    public String getMemory() {
        return result(serverIP, false, true, false);
    }

    @Path("processus")
    @GET
    @Produces("application/xml")
    public String getProcessus() {
        return result(serverIP, false, false, true);
    }

    @Path("all")
    @GET
    @Produces("application/xml")
    public String getAll() {
        return result(serverIP, true, true, true);
    }

    private String result(String host, boolean cpu, boolean memory, boolean processus) {
        String xml = "";
        try {
            //TODO return proper representation object
            Server obj_Server = (Server) Naming.lookup("//" + serverIP + "/Server");
            Monitoring obj_Monitoring = obj_Server.createMonitoring();
            xml = xml + "<server>"
                    + "<ip>" + serverIP + "</ip>"
                    + "</server>";

            WS_Database bd = new WS_Database();       
            bd.connect();
            
            if (cpu) {
                xml = xml + "<cpu>"
                        + "<user>" + obj_Monitoring.getCPU().getUserCPU() + "</user>"
                        + "</cpu>";
                bd.execUpdate("INSERT INTO RMIHISTO VALUES (default,'127.0.0.1','CPU','" + obj_Monitoring.getCPU().getUserCPU() + "');");
            }

            if (memory) {
                xml = xml + "<memory>"
                        + "<free>" + obj_Monitoring.getMemory().getFreeMemory() + "</free>"
                        + "</memory>";
                bd.execUpdate("INSERT INTO RMIHISTO VALUES (default,'127.0.0.1','Memory','" + obj_Monitoring.getMemory().getFreeMemory() + "');");
            }

            if (processus) {
                xml = xml + "<processus>";

                for (Proc process : obj_Monitoring.getProcessus().getListOfProcessus()) {
                    xml = xml + "<process>"
                            + "<pid>" + process.getPid() + "</pid>"
                            + "<command>" + process.getCommand() + "</command>"
                            + "<user>" + process.getUser() + "</user>"
                            + "<cpu>" + process.getCpu() + "</cpu>"
                            + "<memory>" + process.getMem() + "</memory>"
                            + "<priority>" + process.getPriorite() + "</priority>"
                            + "</process>";
                }

                xml = xml + "</processus>";
                bd.saveObject(serverIP, obj_Monitoring.getProcessus().getListOfProcessus());
            }
            
            bd.close();

        } catch (Exception e) {
            xml = "<message>"
                    + "<error>"
                    + e
                    + "</error>"
                    + "</message>";
            Logger.getLogger(WS_Live.class.getName()).log(Level.SEVERE, null, e);
        }
        return xmlhead + xml + xmlfoot;
    }

    /**
     * PUT method for updating or creating an instance of WS_Live
     *
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes("application/xml")
    public void putXml(String content) {
    }
}
